package com.scs.application.modules.sys.schedule;

import com.scs.application.core.utils.StringUtils;
import org.springframework.scheduling.quartz.QuartzJobBean;

/**
 */
public abstract class AbstractTask extends QuartzJobBean {

    private Object[] params = null;
    private Class[] parameterTypes = null;

    protected void resolveParameters(String paramsStr) {
        if (StringUtils.isNotBlank(paramsStr)) {
            String[] strings = StringUtils.split(paramsStr, ",");
            params = new Object[strings.length];
            parameterTypes = new Class[strings.length];
            for (int i = 0; i < strings.length; i++) {
                String str = strings[i].trim();
                if ((StringUtils.startsWith(str, "'") && StringUtils.endsWith(str, "'")) ||
                        StringUtils.startsWith(str, "\"") && StringUtils.endsWith(str, "\"")) {
                    params[i] = str.substring(1, str.length() - 1);
                    parameterTypes[i] = String.class;
                } else if (str.endsWith("D")) {
                    params[i] = Double.valueOf(str);
                    parameterTypes[i] = Double.class;
                } else if (str.endsWith("d")) {
                    params[i] = Double.parseDouble(str);
                    parameterTypes[i] = double.class;
                } else if (str.endsWith("F")) {
                    params[i] = Float.valueOf(str);
                    parameterTypes[i] = Float.class;
                } else if (str.endsWith("f")) {
                    params[i] = Float.parseFloat(str);
                    parameterTypes[i] = float.class;
                } else if (StringUtils.isNumeric(str)) {
                    params[i] = Integer.parseInt(str);
                    parameterTypes[i] = int.class;
                } else if ("false".equals(str) || "true".equals(str)) {
                    params[i] = Boolean.parseBoolean(str);
                    parameterTypes[i] = boolean.class;
                }
            }
        }
    }

    protected Object[] getParameters() {
        return params;
    }

    protected Class[] getParametersType() {
        return parameterTypes;
    }

}
